from kubernetes import client, config
import yaml


def parse_k8s_config():
    config_map = None

    # Load the in-cluster config
    config.load_incluster_config()

    # Set the namespace and ConfigMap name
    namespace = 'taihang'  # Change this to the namespace of your ConfigMap
    config_map_name = 'cement-strength-prediction-model-microservice'  # Change this to the name of your ConfigMap
    # Create a client for the CoreV1API
    v1 = client.CoreV1Api()
    try:
        # Read the specified ConfigMap
        config_map = v1.read_namespaced_config_map(name=config_map_name, namespace=namespace)
        print(f"ConfigMap {config_map_name} in namespace {namespace} found.")
        print("Data:", config_map.data)
        config_content = config_map.data.get(f'{config_map_name}.yaml')  # 读取该项目的configMap
        config_dict = yaml.safe_load(config_content)  # 转换为dict类型
        print("===================== config文件参数 ========================")
        print(config_dict)
        # config_dict = config_map.data
    except client.exceptions.ApiException as e:
        if e.status == 404:
            print(f"ConfigMap {config_map_name} in namespace {namespace} not found.")
        else:
            print(f"An error occurred: {e}")
    return config_dict


if __name__ == "__main__":
    parse_k8s_config()